import {TravelModel} from "../../../../models/travel";

const travelModel=new TravelModel();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    hotCities:Array,
    cities:Object,
    goToLetter:{
      type:String,
      observer:function (n, o) {

        let scrollTop=0;
        let titleHeight=0;
        let cityblock=0;
        let query = this.createSelectorQuery();
        query.selectAll('.hot').boundingClientRect(rect=>{
          rect.forEach(function(r) {
            titleHeight+=r.height;
          });
          if(cityblock!=0){
            this.setData({
              scrollTop:cityblock+titleHeight+5
            })
          }

        }).exec();

        let query1 = this.createSelectorQuery();
        query1.selectAll('.list-part').boundingClientRect(rect=>{



          for(let i in rect){

            if(rect[i].id==="p"+n){
              break;
            }
            cityblock+=rect[i].height;
          }

          if(titleHeight!=0){
            this.setData({
              scrollTop:cityblock+titleHeight+5
            })
          }

        }).exec();

        // for (let i in this.properties.cities) {
        //   if(i===n){
        //     break
        //   }
        //   scrollTop +=titleHeight;
        //   cityNumber=this.properties.cities[i].length;
        //   scrollTop+=cityNumber+cityHeight*cityNumber
        // }


      }
    }
  },
  ready:function(){
    const letters=[];

    for (let i in this.properties.cities) {

      letters.push(i)
    }

    this.setData({
      letters,
      scrollTop:"200rpx"
    })
  },
  /**
   * 组件的初始数据
   */
  data: {
    scrollTop:-1,
    letters:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeCity:function(event) {
      const city=event.currentTarget.dataset.city;
      travelModel.setSelectedCity(city);
      wx.navigateBack()
    }
  }
})
